package com.myeasyexcel.constant;


import com.myeasyexcel.response.ResultCode;

/**
 * 自定义异常类
 * 抛出运行时异常

 */
public class CustomException extends RuntimeException{

    /**
     * 错误代码
     */
    private ResultCode resultCode;

    public CustomException(ResultCode resultCode){

        super(resultCode.toJsonString());
        this.resultCode = resultCode;
    }

    /**
     * @Author wumin
     * @Description 支持传入错误码和错误描述来构建CustomException
     * @Date 10:51 2021/12/9
     * @Param [code, message]
     * @return
     **/
    public CustomException(int code, String message){
        super("{"+
                "success:false, code:" + code +
                ", msg:'" + message +'\'' +
                "}");
        this.resultCode = new ResultCode() {
            @Override
            public boolean success() {
                return false;
            }

            @Override
            public int code() {
                return code;
            }

            @Override
            public String message() {
                return message;
            }

            @Override
            public String toJsonString() {
                return "{"+
                        "success:false, code:" + code +
                        ", msg:'" + message +'\'' +
                        "}";
            }
        };
    }

    /**
     * @Author wumin
     * @Description 支持传入ResultCode和错误描述来构建CustomException，这样场景应用于错误描述中存在占位符，是动态的
     * @Date 10:51 2021/12/9
     * @Param [code, message]
     * @return
     **/
    public CustomException(ResultCode resultCode, String message){
        super("{"+
                "success:false, code:" + resultCode.code() +
                ", msg:'" + message +'\'' +
                "}");
        this.resultCode = new ResultCode() {
            @Override
            public boolean success() {
                return false;
            }

            @Override
            public int code() {
                return resultCode.code();
            }

            @Override
            public String message() {
                return message;
            }

            @Override
            public String toJsonString() {
                return "{"+
                        "success:false, code:" + resultCode.code() +
                        ", msg:'" + message +'\'' +
                        "}";
            }
        };
    }

    public ResultCode getResultCode(){
        return this.resultCode;
    }
}
